package com.zwj.batch.api.controller;

import com.zwj.batch.common.Result;
import com.zwj.batch.core.batch.job.UserJob;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author 张文接
 * @version 1.0
 * @createTime 2023-06-26 10:18:00
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserJob userJob;
    @Resource
    private JobLauncher jobLauncher;


    @PostMapping("/read/txt")
    public Result<String> readUserFromTxt() {
        Job readUserTxtJob = userJob.getReadUserTxtJob();
        try {
            jobLauncher.run(readUserTxtJob, new JobParameters());
        } catch (JobExecutionAlreadyRunningException | JobRestartException |
                 JobInstanceAlreadyCompleteException | JobParametersInvalidException e) {
            throw new RuntimeException(e);
        }
        return Result.ok();
    }

    @PostMapping("/read/txt2")
    public Result<String> readUserFromTxt2() {
        Job job = userJob.getReadUserTxtJob2();
        try {
            jobLauncher.run(job, new JobParameters());
        } catch (JobExecutionAlreadyRunningException | JobRestartException |
                 JobInstanceAlreadyCompleteException | JobParametersInvalidException e) {
            throw new RuntimeException(e);
        }
        return Result.ok();
    }

    @PostMapping("/read/json")
    public Result<String> readUserFromJson() {
        Job job = userJob.getReadUserJsonJob();
        try {
            jobLauncher.run(job, new JobParameters());                                         
        } catch (JobExecutionAlreadyRunningException | JobRestartException |
                 JobInstanceAlreadyCompleteException | JobParametersInvalidException e) {
            throw new RuntimeException(e);
        }
        return Result.ok();
    }

    @PostMapping("/read/txt/validate")
    public Result<String> readUserFromJsonValidate() {
        Job job = userJob.getReadUserJsonValidateJob();
        try {
            jobLauncher.run(job, new JobParameters());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.ok();
    }

    @PostMapping("/adapter")
    public Result<String> readUserProcess() {
        Job job = userJob.getProcessUserJob();
        try {
            jobLauncher.run(job, new JobParameters());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.ok();
    }

    @PostMapping("/composite/processor")
    public Result<String> readUserCompositeProcess() {
        Job job = userJob.getCompositeProcessUserJob();
        try {
            jobLauncher.run(job, new JobParameters());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.ok();
    }

    @PostMapping("/customize/processor")
    public Result<String> readUserCustomizeProcess() {
        Job job = userJob.getCustomizeProcessUserJob();
        try {
            jobLauncher.run(job, new JobParameters());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.ok();
    }
}
